from fastapi import APIRouter
from ..model.master import AOI
from ..utils.database import SessionLocal
from ..utils.post_item import UserAllItem
from sqlalchemy import func
router = APIRouter()


# 获取 aoi 服务器数据列表
@router.post('/aoi/all')
async def all_aoi(item: UserAllItem):
    res = []
    page_index = item.page_index
    page_size = item.page_size
    offset_data = page_size * (page_index-1)
    with SessionLocal() as session:
        total_num = session.query(func.count(AOI.id)).scalar()
        aois = session.query(AOI).order_by(AOI.id.desc()).offset(offset_data).limit(page_size).all()
        for aoi in aois:
            aoi_item = {}
            aoi_item["hostname"] = aoi.hostname
            aoi_item["port"] = aoi.port
            aoi_item["username"] = aoi.username
            aoi_item["password"] = aoi.password
            aoi_item["lotdir"] = aoi.lotdir
            aoi_item["logdir"] = aoi.logdir
            aoi_item["server_host"] = aoi.server_host
            aoi_item["aoi_name"] = aoi.aoi_name

            res.append(aoi_item)
    
    return {"data": res, "total_num": total_num}